<?php
namespace app\commission\controller;
use app\ApiRest;
use think\App;
use app\commission\model\Cash as Model;
use app\commission\model\Water;
use app\commission\model\Wallet;

use think\facade\Cache;
use think\facade\Db;

class IndexWallet extends ApiRest
{

    protected $model;

    protected $water_model;

    protected $wallet_model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model        = new Model();

        $this->water_model  = new Water();

        $this->wallet_model = new Wallet();
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-23 15:05
     * @功能说明:申请提现
     */
    public function applyWallet(){

        $input = $this->_input;
        //余额
        $total_price = $this->model->where(['user_id'=>$this->getUserId(),'uniacid'=>$this->_uniacid])->value('total_cash');

        if($total_price<$input['wallet_price']){

            $this->errorMsg('余额不足');
        }
        //订单号
        $out_trade_no = date( 'YmdHis' ) . '0' . $this->_uniacid. '0';

        $idlen        = strlen($this->_uniacid);
        //订单编号
        $order_code   = $out_trade_no . str_repeat( '0', 11 - $idlen ) . rand(0,9999);

        Db::startTrans();

        $insert = [

            'uniacid'     => $this->_uniacid,

            'user_id'     => $this->getUserId(),

            'ready_price' => $input['wallet_price'],

            'true_price'  => $input['wallet_price'],

            'status'      => 1,

            'order_code'  => $order_code,
        ];
        //加提现记录
        $this->wallet_model->walletAdd($insert);

        $this->model->incOrDecCash(['user_id'=>$this->getUserId(),'uniacid'=>$this->_uniacid],$insert['true_price'],2);

        Db::commit();

        return $this->success(1);


    }






}
